package com.hrms.service;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MailHandlerServlet
 *
 * @web.servlet
 *   name="MailHandlerServlet"
 *   display-name="MailHandlerServlet"
 *
 * @web.servlet-mapping
 *   url-pattern="/_ah/mail/*"
 */
public class MailHandlerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(MailHandlerServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MailHandlerServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Properties props = new Properties(); 
        Session session = Session.getDefaultInstance(props, null); 
        log.info("Receieved msg");
        try {
			MimeMessage message = new MimeMessage(session, request.getInputStream());
			log.info(message.getFrom()[0].toString());
			String statusMsg=message.getSubject();
			
	        log.info(statusMsg);
	        log.info(message.getContent().toString());
	        
			/*Queue q = QueueFactory.getQueue("tweet-queue");			
			q.add(TaskOptions.Builder.withUrl("/tasks/Tweet.do").param("statusMsg", statusMsg));*/
			
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

}
